#region Import Declarations

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using CommunityServer.Components;
using System.Web.UI.WebControls;
using CommunityServer.Controls;
using System.Reflection;
using CommunityServer;

#endregion

namespace CommunityServerStuff.Controls
{
    public class UserExtendedAttributesSubForm : WrappedSubFormBase
    {

        #region Private Members

        private List<string> _extendedAttributes = null;

        #endregion

        #region Properties

        #region IsEnabled

        public override bool IsEnabled()
        {
            return true;
        }

        #endregion

        #region DataSource

        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (value is User || value == null)
                    base.DataSource = value;
                else
                    throw new InvalidCastException("DataSource must be a valid User object");
            }
        }

        #endregion

        #region ExtendedAttributes

        /// <summary>
        /// The ExtendedAttributes
        /// </summary>
        public string ExtendedAttributes
        {
            get { return ((string)ViewState["ExtendedAttributes"]) ?? string.Empty; }
            set
            {
                ViewState["ExtendedAttributes"] = value;
                if (!string.IsNullOrEmpty(value))
                {
                    _extendedAttributes = new List<string>();
                    foreach (string ea in value.Split(','))
                    {
                        if (!string.IsNullOrEmpty(ea.Trim()))
                            _extendedAttributes.Add(ea.Trim());
                    }
                }
            }
        }

        #endregion

        #endregion

        #region Methods

        #region AttachChildControls

        protected override void AttachChildControls()
        {
            //Not implemented
        }

        #endregion

        #region ApplyChangesAfterCommit

        public override void ApplyChangesAfterCommit(object activeObject)
        {
            if (_extendedAttributes == null)
                return;

            EnsureChildControls();

            if (!this.IsValid())
                return;

            User u = activeObject as User;
            if (u == null)
                return;

            foreach (string controlName in _extendedAttributes)
            {
                Control c = CSControlUtility.Instance().FindControl(this, controlName);
                if (c != null)
                {
                    string value = null;

                    if (c is TextBox)
                        value = ((TextBox)c).Text.Trim();
                    else if (c is CheckBox)
                        value = ((CheckBox)c).Checked.ToString();
                    else if (c is ListControl)
                        value = ((ListControl)c).SelectedValue;

                    if (value != null)
                    {
                        try
                        {
                            u.SetExtendedAttribute(c.ID, value); 
                            string message = String.Format("UserExtendedAttribute Saved. {0} = {1}.", c.ID, value);
                            EventLogs.Warn(message, "Registration", 301);
                        }
                        catch (Exception ex)
                        {
                            CSException csex = new CSException(CSExceptionType.UnknownError, null, ex);
                            csex.Log(CSContext.Current.SettingsID);
                        }
                    }
                }
            }
            Users.UpdateUser(u);
        }

        #endregion

        #endregion

    }
}